package com.dt.dubai.gaug.controller.innate;

import com.dt.dubai.gaug.entity.GaugEntrustReportEntity;
import com.dt.dubai.gaug.service.ser.GaugEntrustReportService;
import com.alibaba.fastjson.TypeReference;
import com.dt.dubai.core.comm.*;
import com.dt.dubai.core.exception.PageErrorMesg;
import com.dt.dubai.core.utile.I18nUtile;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 委托检测报告记录表
*/
public abstract class GaugEntrustReportInnateAbs {

    @Autowired
    protected I18nUtile i18nUtile;

    @Autowired
    protected GaugEntrustReportService gaugEntrustReportSer;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> addEntity(@RequestParam("data")  String data, final Long userId) {
        try {
            SqlUtils.clearSqlCode(data);
            SqlUtils.clearWebCode(data);
            GaugEntrustReportEntity entity = JSON.parseObject(data, GaugEntrustReportEntity.class);
            userSavEntity(entity, userId);
            gaugEntrustReportSer.savEntity(entity, userId);
            return new PageData().success(entity.getRecordId(), i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
            System.out.println(i18nUtile.getMessage(code));
            e.printStackTrace();
            return PageData.error(code, i18nUtile.getMessage(code));
        }
    }

    @RequestMapping(value = "/upd", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> upEntity(@RequestParam("data")  String data, final Long userId) {
        try {
            SqlUtils.clearSqlCode(data);
            SqlUtils.clearWebCode(data);
            GaugEntrustReportEntity entity = JSON.parseObject(data, GaugEntrustReportEntity.class);
            userModEntity(entity, userId);
            gaugEntrustReportSer.modEntity(entity, userId);
            return new PageData().success("", i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
            System.out.println(i18nUtile.getMessage(code));
            e.printStackTrace();
            return PageData.error(code, i18nUtile.getMessage(code));
        }
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> delByKey(@RequestParam("data")  String data, final Long userId) {
        try {
            Type type = new TypeReference<List<Long>>() {
            }.getType();
            List<Long> list = JSON.parseObject(data, type);
            userDelByKey(list, userId);
            gaugEntrustReportSer.delByKey(list, userId);
            return new PageData().success("", i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
            System.out.println(i18nUtile.getMessage(code));
            e.printStackTrace();
            return PageData.error(code, i18nUtile.getMessage(code));
        }
    }

    @RequestMapping(value = "/findKey", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> queByKey(@RequestParam("data")  String data, final Long userId) {
        try {
            SqlUtils.clearSqlCode(data);
            SqlUtils.clearWebCode(data);
            Type type = new TypeReference<Map<String, Long>>() {
            }.getType();
            Map<String, Long> map = JSON.parseObject(data, type);
            Map<String, Object> result = gaugEntrustReportSer.pageByKey(map.get("recordId"), userId);
            userQueByKey(map.get("recordId"), result, userId);
            return new PageData().success(result, i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
            System.out.println(i18nUtile.getMessage(code));
            e.printStackTrace();
            return PageData.error(code, i18nUtile.getMessage(code));
        }
    }

    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> quePageList(@RequestParam("data")  String data, final Long userId) {
        try {
            SqlUtils.clearSqlCode(data);
            SqlUtils.clearWebCode(data);
            PageParameterData queryCondition = JSON.parseObject(data, PageParameterData.class);
            if (queryCondition == null) {
                queryCondition = new PageParameterData(ConstantValue.INT_1, ConstantValue.INT_10);
            }
            PageListData<Map<String, Object>> page = gaugEntrustReportSer.pageList(queryCondition, queryCondition.getParamList(),userId);
            userQuePageList(queryCondition, queryCondition.getParamList(), page.getList(), userId);
            return new PageData().success(page, i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } catch (RuntimeException e) {
            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
            System.out.println(i18nUtile.getMessage(code));
            e.printStackTrace();
            return PageData.error(code, i18nUtile.getMessage(code));
        }
    }

    protected abstract void userSavEntity(GaugEntrustReportEntity inEntity, final Long userId);
    protected abstract void userModEntity(GaugEntrustReportEntity inEntity, final Long userId);
    protected abstract void userDelByKey(List<Long> recordIds, final Long userId);
    protected abstract void userQueByKey(Long recordId, Map<String, Object> map, final Long userId);
    protected abstract void userQuePageList(PageParameterData queryCondition, List<PageCondition> joinCondition, List<Map<String, Object>> resultMap, final Long userId);

}
